﻿package Cain.mc_cesh
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 拍照,可傳入影片和要拍的範圍,回傳BitmapData
	 * @author cain@ 2009
	 */
	public class Cesher 
	{
		private var mc:DisplayObject;
		private var rec:Rectangle;
		private var bit:BitmapData;
		/**
		 * 拍照
		 * @param	_mc 影片
		 * @param	_rec 範圍
		 */
		public function Cesher(_mc:DisplayObject,_rec:Rectangle) 
		{
			mc = _mc;
			rec = _rec;
			init();
		}
		
		private function init():void
		{
			var mc_rec:Rectangle = mc.getRect(mc);
			var newMatrix:Matrix = new Matrix();
			//newMatrix.translate( -mc_rec.x -rec.x, -mc_rec.y-rec.y);
			newMatrix.translate( -rec.x,-rec.y);
			var mc_bit:BitmapData = new BitmapData(rec.width, rec.height, true, 0);			
			mc_bit.draw(mc, newMatrix);
			bit = mc_bit;
		}
		/**
		 * 拍的BitmapData
		 * @return 拍的BitmapData
		 */
		public function  bitmapData():BitmapData
		{
			return bit;
		}
		/**
		 * 將拍的內容暫時顯示在螢幕上(trace用)
		 */
		public function shoit():void 
		{
			var bitda:Bitmap = new Bitmap(bit);
			//MovieClip(mc.root).addChild(bitda);
			Sprite(mc.root).addChild(bitda);
		}
	}
	
}